//
//  NickViewController.m
//  WHSport
//
//  Created by ccjoy-jimneylee on 15/5/8.
//  Copyright (c) 2015年 jimneylee. All rights reserved.
//

#import "NickViewC.h"
#import "PersonalViewC.h"
#import "UserModel.h"
#import "Util.h"
#import <MBProgressHUD.h>

@interface NickViewC ()

@end

@implementation NickViewC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title =@"修改昵称";
    self.navigationItem.rightBarButtonItem =
    [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sureicon.png"]
                                     style:UIBarButtonItemStylePlain
                                    target:self
                                    action:@selector(save:)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)save:(id)sender{
    
     NSString *nick = _nickField.text;
    [Util changeNSString:nick];
    [self.view endEditing:YES];
    NSString  *ID = [NSString stringWithFormat:@"%@",
               [UserModel sharedInstance].currentUser.ID];
    NSString *sex = [NSString stringWithFormat:@"%@",
                     [UserModel sharedInstance].currentUser.sex];
    NSString *address = [UserModel sharedInstance].currentUser.address;
    [[UserModel sharedInstance] personalModifyWithID:ID sex:sex address:address nick:nick
      perMod:^(BOOL successed, unsigned int value) {
          if (successed) {
              if (value == 0) {
                  [UserModel sharedInstance].currentUser.nick = nick;
                   MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:
                                                self.view animated:YES];
                   HUD.labelText = @"昵称修改成功";
                   [HUD hide:YES afterDelay:1.5f];
              }
              
              else{
                  MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                  HUD.labelText = @"昵称修改失败";
                  [HUD hide:YES afterDelay:1.5f];
              }
              [self dismissViewControllerAnimated:YES
                                       completion:^{}];
          }
    }];
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField;
{
    [textField resignFirstResponder];
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
